package com.baizhi.action;

import com.baizhi.entity.Book;
import com.baizhi.entity.Category;
import com.baizhi.service.BookService;
import com.baizhi.service.CategoryService;
import com.baizhi.service.impl.BookServiceImpl;
import com.baizhi.service.impl.CategoryServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class BookAction {
    private Integer pageNum;
    private Integer pageSize;
    private List<Book> books;
    private PageInfo<Book> pageInfo;
    private Book book;
    private String coverFileName;
    private File cover;

    private String message;

    private String bookId;

    private List<Category> categories;

    private String key;
    private String content;

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    public PageInfo<Book> getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(PageInfo<Book> pageInfo) {
        this.pageInfo = pageInfo;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public String getCoverFileName() {
        return coverFileName;
    }

    public void setCoverFileName(String coverFileName) {
        this.coverFileName = coverFileName;
    }

    public File getCover() {
        return cover;
    }

    public void setCover(File cover) {
        this.cover = cover;
    }

    public String getBookId() {
        return bookId;
    }

    public void setBookId(String bookId) {
        this.bookId = bookId;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String selectAllBookByPages(){
        BookService bookService=new BookServiceImpl();
        if (pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=4;
        }
        PageHelper.startPage(pageNum,pageSize);
        books = bookService.selectAllBookByPages(key,content);
        pageInfo=new PageInfo<>(books,5);
        return "success";
    }

    public String toAddBook(){
        CategoryService categoryService=new CategoryServiceImpl();
        categories = categoryService.selectCategoryLevels2();
        return "addBook";
    }

    public String addBook() throws IOException {
        BookService bookService=new BookServiceImpl();
        HttpServletRequest request = ServletActionContext.getRequest();
        ServletContext servletContext = request.getServletContext();
        String realPath = servletContext.getRealPath("/back/img");

        FileInputStream fis = new FileInputStream(cover);
        String outPath=realPath+"/"+coverFileName;
        FileOutputStream fos = new FileOutputStream(outPath);

        byte [] bytes=new byte[1024];
        int len=0;
        while((len=fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
        fis.close();
        fos.close();
        String uuid = UUID.randomUUID().toString();
        book.setBookId(uuid);
        book.setCreateDate(new Date());
        book.setCover(coverFileName);
        book.setSale(0);
        bookService.addBook(book);
        return "showBookList";
    }

    public String toUpdateBook(){
        BookService bookService=new BookServiceImpl();
        CategoryService categoryService=new CategoryServiceImpl();
        categories = categoryService.selectCategoryLevels2();
        book=bookService.selectBookById(bookId);
        return "updateBook";
    }

    public String updateBook() throws IOException {
        BookService bookService=new BookServiceImpl();
        if (cover==null){
            bookService.updateBook(book);
        }else {
            HttpServletRequest request = ServletActionContext.getRequest();
            ServletContext servletContext = request.getServletContext();
            String realPath = servletContext.getRealPath("/back/img");
            FileInputStream fis = new FileInputStream(cover);
            String outPath=realPath+"/"+coverFileName;
            FileOutputStream fos = new FileOutputStream(outPath);
            byte [] bytes=new byte[1024];
            int len=0;
            while((len=fis.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
            fis.close();
            fos.close();
            book.setCover(coverFileName);
            bookService.updateBook(book);
        }
        return "updateBookSuccess";
    }

    public String deleteBook() throws UnsupportedEncodingException {
        BookService bookService=new BookServiceImpl();
        Book book = bookService.selectBookById(bookId);
        if(book!=null&&book.getSale()==0){
            bookService.deleteBookById(bookId);
            return "showBookList";
        }else {
            message= URLEncoder.encode( "当前图书已有销量，不能删除","utf-8");
            return "deleteBookError";
        }
    }
}
